package cn.kgc.controller;

/*
 *@author zhangtao
 *@Date 2023/4/27
 */

import cn.kgc.bean.pojo.EpDangerous;
import cn.kgc.bean.vo.Result;
import cn.kgc.service.EpDangerousService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("danger")
public class EpDangerousController {

    @Resource
    private EpDangerousService epDangerousService;

    /*
     *@author zhangtao
     *@Date 2023/4/27
     * 隐患录入(新增隐患)
     */
    @PostMapping(value = "/addEpDangerous")
    public Result addEpDangerous(@RequestBody EpDangerous epDangerous) {
        log.info(epDangerous.toString());
        boolean flag = epDangerousService.addEpDangerous(epDangerous);
        if (flag) {
            return Result.success();
        } else {
            return Result.failure();
        }
    }


    /*
     *@author zhangtao
     *@Date 2023/4/27
     * 隐患管理(分页展示隐患)
     */

    @GetMapping("/getAllInfo/{pageFrom}/{pageSize}")
    public Result<PageInfo> getAllInfo(@PathVariable Integer pageFrom,
                                       @PathVariable Integer pageSize) {
        log.info(pageFrom.toString());
        PageHelper.startPage(pageFrom, pageSize);
        List<EpDangerous> list = epDangerousService.getAllEpDangerousInfo();
        PageInfo pageInfo = new PageInfo(list);
        log.info(pageInfo.toString());
        return Result.success(pageInfo);
    }


    /*
     *@author zhangtao
     *@Date 2023/4/27
     * 隐患管理(删除隐患)
     * Restful风格
     */
    @PostMapping("/delEpDangerousInfo/{id}")
    public Result delEpDangerousInfo(@PathVariable Integer id) {
        boolean flag = epDangerousService.delEpDangerousInfoById(id);
        if (flag) {
            return Result.success();
        } else {
            return Result.failure();
        }
    }


    /*
     *@author zhangtao
     *@Date 2023/4/27
     * 隐患管理(修改隐患)
     */
    @PostMapping("/updateEpDangerousInfo")
    public Result updateEpDangerousInfo(@RequestBody EpDangerous epDangerous) {
        boolean flag = epDangerousService.updateEpDangerousInfo(epDangerous);
        if (flag) {
            return Result.success();
        } else {
            return Result.failure();
        }
    }

    /*
     *@author zhangtao
     *@Date 2023/4/27
     * 隐患管理(模糊查询隐患)
     *String department, Integer riskLevel, String fixStaff, String checkStaff,String deadline
     */

    @GetMapping (value = "/getSomeInfo")
    public Result getSomeInfo(@RequestParam(defaultValue = "1") Integer pageFrom,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              @RequestParam(value = "department",required = false) String department,
                              @RequestParam(value = "riskLevel",required = false) Integer riskLevel,
                              @RequestParam(value = "fixStaff",required = false) String fixStaff,
                              @RequestParam(value = "checkStaff",required = false) String checkStaff,
                              @RequestParam(value = "startline",required = false) String startline,
                              @RequestParam(value = "endline",required = false) String endline) {
        PageHelper.startPage(pageFrom, pageSize);
        log.info("========================"+department,riskLevel,fixStaff,checkStaff,startline,endline);
        List<EpDangerous> list = epDangerousService.getSomeDangerousInfo(department, riskLevel, fixStaff, checkStaff,startline,endline);
        PageInfo pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }



    /*
     *@author zhangtao
     *@Date 2023/4/27
     * 隐患管理(根据隐患时间模糊查询)
     */
    @GetMapping (value = "/getTimeInfo/{pageFrom}/{pageSize}")
    public Result getTimeInfo(@PathVariable Integer pageFrom,
                              @PathVariable Integer pageSize,
                              @RequestParam(value = "startline",required = false) String startline,
                              @RequestParam(value = "endline",required = false) String endline) {
        PageHelper.startPage(pageFrom, pageSize);
        List<EpDangerous> list = epDangerousService.getInfoByTime(startline,endline);
        PageInfo pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }
    @RequestMapping ("/getNum")
    public Result getNum() {
        List<EpDangerous> list = epDangerousService.getNum();
        log.info(list.toString());
        return Result.success(list);
    }

    @RequestMapping("/getEpDangAll")
    private Result getEpDangAll(@RequestParam String staffId){
        List<EpDangerous> list = epDangerousService.getEpDangAll(staffId);
        return Result.success(list);
    }
}
